package pl.vanta.utils.gui.tables.renderers;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class MinMaxColorRenderer extends DefaultTableCellRenderer
{
	private static final long	serialVersionUID	= 1L;

	private Color				minColor;
	private Color				maxColor;

	public MinMaxColorRenderer(Color minColor, Color maxColor)
	{
		this.minColor = minColor;
		this.maxColor = maxColor;
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
			int row, int column)
	{
		if (value instanceof Number)
		{
			Number number = (Number) value;

			int intValue = number.intValue();
			// int truncValue = Math.min(255, Math.abs(intValue));

			if (intValue > 0)
			{
				setBackground(this.maxColor);
			}
			else if (intValue < 0)
			{
				setBackground(this.minColor);
			}
			else
			{
				if (isSelected)
				{
					setBackground(table.getSelectionBackground());
				}
				else
				{
					setBackground(table.getBackground());
				}

			}
		}

		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

		setHorizontalAlignment(RIGHT);

		return this;
	}
}